package com.zgkxzx.java.mutilThread.PCModel;

import android.util.Log;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 创建者 zgkxzx
 * 日期   12/29/17
 * 描述
 */

public class EventModel extends Model{
    private final static String TAG = EventModel.class.getSimpleName();

    private List<Date> events;
    private int max = 10;

    public EventModel() {
        events = new LinkedList<>();
    }


    @Override
    public synchronized void putEvent() {
        while (events.size() == max) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Date date = new Date();
        events.add(date);
        Log.d(TAG, "添加一个事件 " + date);
        notify();
    }
    @Override
    public synchronized void getEvent() {
        while (events.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Date poll = (Date) ((LinkedList) events).poll();
        Log.d(TAG, "push 一个事件" + poll + " 还有ge " + events.size());
        notify();

    }


}
